//
//  MBScaledImageView.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2018/1/22.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBScaledImageView.h"
#import "MBCircelProgressView.h"

@interface MBScaledImageView()<UIScrollViewDelegate>

@property (nonatomic,weak) UIImageView * contentImageView;
@property (nonatomic,assign) BOOL isScaled;
@property (nonatomic,strong) MBCircelProgressView * progressView;

@end

@implementation MBScaledImageView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        self.backgroundColor = [UIColor clearColor];
        self.contentSize = CGSizeMake(frame.size.width, frame.size.height);
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.delegate = self;
        self.minimumZoomScale = 1.0;
        self.maximumZoomScale = 3.0;
        [self setZoomScale:1.0];
        
        UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap)];
        tapGestureRecognizer.numberOfTapsRequired = 2;
        
        UIImageView * contentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [contentImageView setContentMode:UIViewContentModeScaleAspectFit];
        contentImageView.userInteractionEnabled = YES;
        [self addSubview:contentImageView];
        self.contentImageView = contentImageView;
        [contentImageView addGestureRecognizer:tapGestureRecognizer];
        
        MBCircelProgressView * progressView = [[MBCircelProgressView alloc]init];
        progressView.hidden = YES;
        [self addSubview:progressView];
        self.progressView = progressView;
        
        [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
    }
    return self;
}

- (void)setContentImage:(UIImage *)contentImage
{
    _contentImage = contentImage;
    self.contentImageView.image = contentImage;
}


#pragma mark - 设置进度
- (void)setProgress:(CGFloat)progress
{
    if(progress == 0){
        self.progressView.hidden = NO;
    }
    
    [self.progressView setProgress:progress];
    
    if(progress == 1){
        [self.progressView removeFromSuperview];
    }
}

#pragma mark - ScrollView delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    for (UIView * view in scrollView.subviews){
        return view;
    }
    return nil;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.isScaled = YES;
}

#pragma mark - 双击缩放或缩小
- (void)handleDoubleTap
{
    if(self.isScaled){
        [self setZoomScale:1.0 animated:YES];
    }else{
        [self setZoomScale:2.0 animated:YES];
    }
    self.isScaled = !self.isScaled;
}

#pragma mark - 还原到原始缩放比例
- (void)resetZoomScale
{
    [self setZoomScale:1.0 animated:NO];
}


@end
